<style>
    #febs-product xm-select {
        min-height: 30px;
        line-height: 30px;
    }

</style>

<div class="layui-fluid layui-anim febs-anim" id="febs-product" lay-title="添加菜品">
    <div class="layui-row layui-col-space8 febs-container">
        <div class="layui-col-md12 layui-col-sm12 layui-col-xs12">
            <div class="layui-card" style="margin: 0 auto;">
                <div class="layui-card-header">添加商品</div>
                <div class="layui-card-body user-profile-card">

                    <form class="layui-form" action="" lay-filter="product-type-add">
                        <div class="layui-row">
                            <div class="layui-col-md10">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label">商品名称：</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="productType" id="productType" class="layui-input">
                                        </div>
                                    </div>
                                    <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit
                                            lay-filter="product-type-submit" id="submit">
                                        <i style="display: none; margin-left: 30px"
                                           class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i>
                                        添加商品
                                    </button>
                                </div>
                            </div>
                        </div>
                    </form>

                    <table lay-filter="productTable" lay-data="{id: 'productTable'}"></table>

                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="user-option">
    <!--    <a lay-event="detail">-->
    <!--        <i class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a>-->
<!--    <a lay-event="edit"><i-->
<!--            class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>-->
    <a lay-event="del"><i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
</script>

<script data-th-inline="none" type="text/javascript">
    layui.use(['upload', 'jquery', 'febs', 'form', 'table'], function () {
        let $ = layui.jquery,
            febs = layui.febs,
            table = layui.table,
            form = layui.form,
            $view = $('#febs-product'),
            tableIns


        form.render();

        initproductTable();

        form.on('submit(product-type-submit)', function (data) {
            if (!data.field.productType) {
                febs.alert.warn('请输入商品名称');
                return false;
            }

            febs.post(ctx + 'web/add/producttype', {
                title: data.field.productType
            }, function () {
                febs.alert.success('添加成功');
                window.location.reload();
            })


            return false;
        });

        function initproductTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'foodTable',
                url: ctx + 'web/get/producttype',
                method: 'post',
                page: false,
                totalRow: true,
                parseData: function (res) { //res 即为原始返回的数据
                    return {
                        "code": 0, //解析接口状态
                        "msg": "", //解析提示文本
                        data: res.data //解析数据列表
                    };
                },
                cols: [[
                    {field: 'id', title: 'id'},
                    {field: 'name', title: '商品名称'},
                    {title: '操作', toolbar: '#user-option', width: 120}
                ]]
            });
        }

        table.on('tool(productTable)', function (obj) {
            let data = obj.data,
                layEvent = obj.event;
            console.log(data)
            if (layEvent === 'del') {
                febs.modal.confirm('删除商品', '确定删除该商品？', function () {
                    febs.post(ctx + 'web/delete/producttype', {id: data.id}, function () {
                        febs.alert.success('删除商品成功');
                        layer.closeAll();
                        window.location.reload();
                    });
                });
            }
        });
    })


</script>